package com.lyw.utils;

import com.lyw.constants.GlobalVariables;
import com.lyw.enums.DirectionEnum;
import com.lyw.enums.DuplicateEnum;
import com.lyw.handle.*;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SmallMapUtil {

  public static int number = 0;
  public static final int[] POSITION = {703, 47};
  public static final int[] WIDTH_HEIGHT = {90, 72};
  public static final int INTERVAL = 18;
  //    public static final int[] CUR_CHARACTER_COLOR = {255, 255, 255};
  public static final int[] CUR_CHARACTER_COLOR = {0, 255, 0};
  public static Rectangle mapRectangle;
  static Mat miniMapRole;

  public static void prepareMapRectangle(Rectangle rectangle) {
    mapRectangle = new Rectangle((int) rectangle.getX() + POSITION[0], (int) rectangle.getY() + POSITION[1], WIDTH_HEIGHT[0], WIDTH_HEIGHT[1]);
    miniMapRole = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\TPlan\\TPlan\\src\\main\\resources\\images\\characters\\mini_character.png");
  }

  private SmallMapUtil() {
  }

  public static DirectionEnum getSmallMapStatus(Robot robot, Rectangle rectangle, DuplicateEnum duplicateEnum) {

    return null;
  }

  /**
   * 是否通关
   *
   * @param robot         机器人
   * @param duplicateInfo 副本信息
   */
  public static boolean whetherClearance(Robot robot, DuplicateEnum duplicateInfo) {
    int row, count = 0;
    int[] rgb = robot.createScreenCapture(mapRectangle)
            .getRGB(0, 0, WIDTH_HEIGHT[0], WIDTH_HEIGHT[1],
                    null, 0, WIDTH_HEIGHT[0]);

    int length = rgb.length;
    for (int i = 4 + ((INTERVAL - 3) * WIDTH_HEIGHT[0]); i < length; ) {
      if (((rgb[i] >> 16) & 0xff) == CUR_CHARACTER_COLOR[0]
              && ((rgb[i] >> 8) & 0xff) == CUR_CHARACTER_COLOR[1]
              && (rgb[i] & 0xff) == CUR_CHARACTER_COLOR[2]) {
        count++;
        if (count > 1) {
          return true;
        }
      }

      row = i / WIDTH_HEIGHT[0];
      i += INTERVAL;
      if (i / WIDTH_HEIGHT[0] != row) {
        i += INTERVAL * WIDTH_HEIGHT[0];
      }
    }
    return false;
  }

  public static int[] getTheMiniMapWhereTheCharacterIsLocated(Robot robot, DuplicateEnum duplicateInfo) {
    BufferedImage screenCapture = robot.createScreenCapture(mapRectangle);
    Mat mat = OpenCvUtil.bufferedImageToMat(screenCapture);
    int[] position = OpenCvUtil.getMiniCharacter(mat);

    if (number == 2) {
      number = 0;
      return OpenCvUtil.getBossPosition(mat, duplicateInfo.getMapSize());
    }

    if (position[0] == -1) {
      number++;
      return getTheMiniMapWhereTheCharacterIsLocated(robot, duplicateInfo);
    }

    int mini_map_width = duplicateInfo.getMapSize();
    int x = position[0] / mini_map_width;
    int y = position[1] / mini_map_width;
    return new int[]{x, y};
  }

  /**
   * 获取下一个房间的防线
   */
  public static DataFilteringAdapter getNextRoom(DuplicateEnum duplicateInfo) throws IOException {

    int[] cur_position = GlobalVariables.getMini_map_position();
    int[] nextRoomPosition = duplicateInfo.getNextRoomPosition(cur_position);
    GlobalVariables.setNext_room_position(nextRoomPosition);
    if (nextRoomPosition[0] == cur_position[0]) {
      if (nextRoomPosition[1] < cur_position[1]) {
        return new TopProcessor();
      } else {
        return new BottomProcessor();
      }
    } else {
      if (nextRoomPosition[0] > cur_position[0]) {
        return new RightProcessor();
      } else {
        return new LeftProcessor();
      }
    }
  }

  public static Mat getBossPosition(Robot robot) {
    BufferedImage screenCapture = robot.createScreenCapture(mapRectangle);
    return OpenCvUtil.bufferedImageToMat(screenCapture);
  }
}
